package
{
import flash.events.Event;
import flash.events.MouseEvent;

import mx.controls.CheckBox;
import mx.controls.DataGrid;
import mx.core.Application;
	public class CheckBoxItemRenderer extends CheckBox{
		public function CheckBoxItemRenderer(){
			super();
			this.addEventListener(MouseEvent.CLICK, onCheckBoxItem);
			NotifyCheckboxEvent.dispatcher.addEventListener(NotifyCheckboxEvent.UPDATECHECKBOX_ITEM, updateItemCheckbox);
		}
		
		override public function validateProperties():void{
			super.validateProperties();
			if (listData){
				var dg:DataGrid = DataGrid(listData.owner);
				var column:CheckBoxHeaderColumn = dg.columns[listData.columnIndex];
				column.addEventListener("click",columnHeaderClickHandler);
			}
		}
		
		private function columnHeaderClickHandler(event:MouseEvent):void{
			selected = event.target.selected;
		}
	
		private function onCheckBoxItem(evt:Event):void{
			var app:Application = this.parentApplication as Application;
			var sels:Array = app['selecteds'];
			if((app['dg'].selectedItem as XML).name() == "newrow") return;
			var id:String = app['dg'].selectedItem.@rowId;
			for(var i:int = sels.length - 1; i >= 0 ; i--){
				if(id == sels[i]){
					if(!this.selected){
						sels.splice(i, 1);
						break;
					}
				}
			}
			if(this.selected){
				sels.push(id);
			}
		}
		
		private function updateItemCheckbox(evt:NotifyCheckboxEvent):void{
			selected = evt.data.selected;
		}
	}
}